﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="SysAdmin_Menu_List" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>菜单管理</title>
</head>
<body>
    <script type="text/javascript">
        var iconData = [{
            value: '',
            text: '默认'
        }, {
            value: 'icon-add',
            text: 'icon-add'
        }, {
            value: 'icon-edit',
            text: 'icon-edit'
        }, {
            value: 'icon-remove',
            text: 'icon-remove'
        }, {
            value: 'icon-save',
            text: 'icon-save'
        }, {
            value: 'icon-cut',
            text: 'icon-cut'
        }, {
            value: 'icon-ok',
            text: 'icon-ok'
        }, {
            value: 'icon-no',
            text: 'icon-no'
        }, {
            value: 'icon-cancel',
            text: 'icon-cancel'
        }, {
            value: 'icon-reload',
            text: 'icon-reload'
        }, {
            value: 'icon-search',
            text: 'icon-search'
        }, {
            value: 'icon-print',
            text: 'icon-print'
        }, {
            value: 'icon-help',
            text: 'icon-help'
        }, {
            value: 'icon-undo',
            text: 'icon-undo'
        }, {
            value: 'icon-redo',
            text: 'icon-redo'
        }, {
            value: 'icon-back',
            text: 'icon-back'
        }, {
            value: 'icon-sum',
            text: 'icon-sum'
        }, {
            value: 'icon-tip',
            text: 'icon-tip'
        }];
        $(function () {
            $('#menu_treegrid').treegrid({
                url: 'Menu/List.aspx?paramType=QueryTreeMenu',
                idField: 'key',
                treeField: 'value',
                fit: true,
                fitColumns: true,
                border: false,
                frozenColumns: [[{
                    title: '编号',
                    field: 'key',
                    width: 150,
                    hidden: true
                }, {
                    field: 'value',
                    title: '菜单名称',
                    width: 150
                }]],
                columns: [[{
                    field: 'iconCls',
                    title: '图标',
                    width: 30,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        } else {
                            return formatString('<span class="icon {0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>', value);
                        }
                    }
                }, {
                    field: 'menuCode',
                    title: '菜单标识',
                    width: 200
                }, {
                    field: 'url',
                    title: '菜单路径',
                    width: 200
                }, {
                    field: 'indexNum',
                    title: '排序',
                    width: 50
                }, {
                    field: 'isLeaf',
                    title: '操作',
                    width: 50,
                    formatter: function (value, row, index) {
                        return formatString('<img onclick="sysadmin_menu_edit(\'{0}\');" src="{1}"/>&nbsp;<img onclick="sysadmin_menu_del(\'{2}\');" src="{3}"/>', row.key, 'Images/pencil.png', row.key, 'Images/cancel.png');
                    }
                }]],
                toolbar: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        sysadmin_menu_add();
                    }
                }, '-', {
                    text: '展开',
                    iconCls: 'icon-redo',
                    handler: function () {
                        var node = $('#menu_treegrid').treegrid('getSelected');
                        if (node) {
                            $('#menu_treegrid').treegrid('expandAll', node.cid);
                        } else {
                            $('#menu_treegrid').treegrid('expandAll');
                        }
                    }
                }, '-', {
                    text: '折叠',
                    iconCls: 'icon-undo',
                    handler: function () {
                        var node = $('#menu_treegrid').treegrid('getSelected');
                        if (node) {
                            $('#menu_treegrid').treegrid('collapseAll', node.cid);
                        } else {
                            $('#menu_treegrid').treegrid('collapseAll');
                        }
                    }
                }, '-', {
                    text: '刷新',
                    iconCls: 'icon-reload',
                    handler: function () {
                        $('#menu_treegrid').treegrid('reload');
                    }
                }]
                //,
                //onContextMenu: function (e, row) {
                //    e.preventDefault();
                //    $(this).treegrid('unselectAll');
                //    $(this).treegrid('select', row.id);
                //    $('#menu_treegrid_menu').menu('show', {
                //        left: e.pageX,
                //        top: e.pageY
                //    });
                //}
            });
        });

        //添加菜单
        function sysadmin_menu_add() {

            var parKey = "Root";
            var parValue = "根目录";
            var treeSel = $('#menu_treegrid').treegrid('getSelected');
            if (treeSel) {
                parKey = treeSel.key;
                parValue = treeSel.value;
            }

            $('<div/>').dialog({
                href: 'Menu/Add.aspx?menu_key=' + parKey + '&menu_name=' + escape(parValue),
                width: 600,
                height: 400,
                modal: true,
                title: '菜单添加',
                buttons: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        var d = $(this).closest('.window-body');
                        $('#admin_cdglAdd_addForm').form('submit', {
                            url: 'Menu/List.aspx?paramType=AddMenu',
                            success: function (result) {

                                try {
                                    var r = $.parseJSON(result);
                                    if (r.success) {

                                        //$('#menu_treegrid').treegrid('append', {
                                        //    parent: r.obj.pid,
                                        //    data: [r.obj]
                                        //});

                                        d.dialog('destroy');
                                        //增加成功，刷新列表
                                        $('#menu_treegrid').treegrid('reload');
                                    }
                                } catch (e) {

                                    $.messager.alert('提示', "增加失败，请重试！");
                                }
                            }
                        });
                    }
                }],
                onClose: function () {
                    $(this).dialog('destroy');
                }
            });
        }

        //编辑菜单
        function sysadmin_menu_edit(menu_key) {
            $('<div/>').dialog({
                href: 'Menu/Edit.aspx?menu_key=' + menu_key,
                width: 600,
                height: 400,
                modal: true,
                title: '菜单编辑',
                buttons: [{
                    text: '编辑',
                    iconCls: 'icon-add',
                    handler: function () {
                        var d = $(this).closest('.window-body');
                        $('#admin_menu_editForm').form('submit', {
                            url: 'Menu/List.aspx?paramType=UpdateModel',
                            success: function (result) {

                                try {
                                    var r = $.parseJSON(result);
                                    if (r.success) {
                                        d.dialog('destroy');

                                        $('#menu_treegrid').treegrid('reload');
                                    }
                                } catch (e) {

                                    $.messager.alert('提示', "增加失败，请重试！");
                                }
                            }
                        });
                    }
                }],
                onClose: function () {
                    $(this).dialog('destroy');
                }
            });
        }

        //删除菜单
        function sysadmin_menu_del(menu_key) {
            ////先判断是否选中节点
            //var nodeData = $('#menu_treegrid').treegrid('getSelected');
            //if (nodeData) {
            $.messager.confirm('提示', '你确定要删除该行数据么?', function (r) {
                if (r) {
                    $.ajax({
                        type: "POST",
                        url: 'Menu/List.aspx',
                        data: "paramType=DelMenu&menuCode=" + menu_key,
                        dataType: "json",
                        success: function (msg) {
                            if (msg.success) {
                                $('#menu_treegrid').treegrid('reload');
                            }
                        },
                        error: function (e, ex) {
                            $.messager.alert('错误提示', '删除失败！');
                        }

                    });
                }
            });
            //}
            //else {
            //    $.messager.alert('信息提示', '请选择一个节点！');
            //}

        }

        ///添加根节点
        function AddRootNode() {
            document.getElementById("parent_code").value = "Root";
            document.getElementById("menu_level").value = "0";
            $('#divAddMenu').dialog('open');
        }
        function AddChildNode() {
            //选中的节点
            var nodeData = $('#treeMenu').tree('getSelected');
            if (nodeData) {
                var att = nodeData.attributes;
                document.getElementById("parent_code").value = nodeData.id;
                document.getElementById("menu_level").value = att.menu_level;
                $('#divAddMenu').dialog('open');
            }
            else {
                $.messager.alert('信息提示', '请选择一个节点！');
            }
        }

        //树形组织重新绑定
        function treeRefeshData() {

            $('#treeMenu').tree('reload');
        }

        //删除节点
        function delModel() {
            //先判断是否选中节点
            var nodeData = $('#treeMenu').tree('getSelected');
            if (nodeData) {
                $.messager.confirm('提示', '你确定要删除该行数据么?', function (r) {
                    if (r) {
                        $.ajax({
                            type: "POST",
                            url: 'List.aspx',
                            data: "paramType=DelMenu&menuCode=" + nodeData.id,
                            dataType: "json",
                            success: function (msg) {
                                treeRefeshData();
                                $('#tbCode').val("");
                                $('#tbName').val("");
                                $('#tbNum').val("");
                                document.getElementById("tbIsActive").checked = true;
                                $('#tbUrl').val("");
                                $('#tbIconName').val("");
                                document.getElementById("menu_code").value = "";
                            },
                            error: function (e, ex) {
                                $.messager.alert('错误提示', '删除失败！');
                            }

                        });
                    }
                });
            }
            else {
                $.messager.alert('信息提示', '请选择一个节点！');
            }
        }

        function delModelByCode(key) {

        }

        ///更新节点
        function updateModel() {
            if (document.getElementById("menu_code").value != "") {
                $.ajax({
                    type: "POST",
                    url: 'List.aspx?' + "paramType=UpdateModel&menuCode=" + document.getElementById("menu_code").value,
                    data: sy.serializeObject($('#FormUpdate').form()),
                    dataType: "json",
                    success: function (msg) {
                        $.messager.alert('信息提示', '修改成功！');
                        //刷新父框架中的树节点
                        parent.treeRefeshData();
                    },
                    error: function (e, ex) {
                        $.messager.alert('信息提示', '修改失败！');
                    }
                });
            }
            else {
                $.messager.alert('信息提示', '请在左侧菜单列表中选择一个要修改的节点');
            }
        }
    </script>

    <table id="menu_treegrid"></table>
    <%--    <div id="menu_treegrid_menu" class="easyui-menu" style="width: 120px; display: none;">
        <div onclick="sysadmin_menu_add();" data-options="iconCls:'icon-add'">增加</div>
        <div onclick="sysadmin_menu_add();" data-options="iconCls:'icon-remove'">删除</div>
        <div onclick="sysadmin_menu_add();" data-options="iconCls:'icon-edit'">编辑</div>
    </div>--%>
</body>
</html>
